package com.seeTools.next.controller.sys;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysOss;
import com.seeTools.next.api.vo.SysOssVo;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.core.Result;
import com.seeTools.next.service.sys.SysOssService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * OSS对象存储控制器
 * 前端访问路由地址为:/system/oss
 *
 * @author seeTools
 * @date 2024-01-20 15:49:01
 */
@Validated
@RestController
@RequestMapping("/oss")
public class SysOssController {

    @Resource
    private SysOssService sysossService;

    /**
     * 查询OSS对象存储列表
     */
    @SaCheckPermission("system:oss:list")
    @PostMapping("/list")
    public Result<Page<SysOss>> list(@RequestBody PageQuery<SysOss> pageQuery) {
        Page<SysOss> pageInfo = sysossService.findList(pageQuery);
        return Result.success(pageInfo);
    }

    /**
     * 导出OSS对象存储列表
     */
    @SaCheckPermission("system:oss:export")
//    @Log(title = "OSS对象存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOss model, HttpServletResponse response) {
        List<SysOss> list = sysossService.selectDataList(model);
//        ExcelUtil.exportExcel(list, "OSS对象存储", SysOss.class, response);
    }

    /**
     * 获取OSS对象存储详细信息
     *
     * @param ossId 主键
     */
    @SaCheckPermission("system:oss:query")
    @GetMapping("/{ossId}")
    public Result<SysOss> getInfo(@PathVariable Long ossId) {
        return Result.success(sysossService.getById(ossId));
    }

    /**
     * 新增OSS对象存储
     */
    @SaCheckPermission("system:oss:add")
//    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping()
    public Result<Void> add(@Validated @RequestBody SysOss model) {
        return Result.state(sysossService.save(model));
    }

    /**
     * 修改OSS对象存储
     */
    @SaCheckPermission("system:oss:edit")
//    @Log(title = "OSS对象存储", businessType = BusinessType.UPDATE)
    @PutMapping()
    public Result<Void> edit(@Validated @RequestBody SysOss model) {
        return Result.state(sysossService.updateOnLock(model));
    }

    /**
     * 删除OSS对象存储
     *
     * @param SysOss 對象
     */
    @SaCheckPermission("system:oss:remove")
//    @Log(title = "OSS对象存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public Result<Void> remove(@RequestBody SysOss sysOss) {
        return Result.state(sysossService.removeById(sysOss));
    }


    @SaCheckPermission("system:oss:upload")
//    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return Result.error("上传文件不能为空");
        }
        SysOssVo oss = sysossService.upload(file);
        if (ObjectUtil.isNotEmpty(oss)) {
            Map<String, String> map = new HashMap<>(2);
            map.put("url", oss.getUrl());
            map.put("fileName", oss.getOriginalName());
            map.put("ossId", oss.getOssId().toString());
            return Result.success(map);
        }
        return Result.error();
    }
}

